package cn.jboa.service.impl;

import cn.jboa.dao.EmployeeDao;
import cn.jboa.entity.Employee;
import cn.jboa.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.Iterator;
import java.util.List;

@Service("employeeService")
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    @Qualifier("employeeDao")
    private EmployeeDao employeeDao;

    public void setEmpDao(EmployeeDao employeeDao) {
        this.employeeDao = employeeDao;
    }

    public Employee login(Employee employee) {
        try {
            List<Employee> employees = employeeDao.findEmployeeByProperty("sn", employee.getSn());
            Iterator<Employee> it = employees.iterator();
            if (it.hasNext()) {
                employee = (Employee) it.next();
                if (employee != null && employee.getPassword().equals(employee.getPassword()))
                    return employee;
            }
            return null;
        } catch (RuntimeException e) {
            e.printStackTrace();
            return null;
        }
    }

    public Employee getManager(Employee employee) {
        return employeeDao.getManager(employee);
    }

    public EmployeeDao getEmployeeDao() {
        return employeeDao;
    }

    public void setEmployeeDao(EmployeeDao employeeDao) {
        this.employeeDao = employeeDao;
    }
}
